﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BluSteamClient.View
{
    /// <summary>
    /// Interaction logic for ChatView.xaml
    /// </summary>
    public partial class UserChatView : UserControl
    {
        public UserChatView()
        {
            InitializeComponent();
            makeStartPage();
        }

        private void StartConversation_Click(object sender, RoutedEventArgs e)
        //This method is attached the click event on the start conversation menu context button
        {
            //ListBoxItem selectedUser = (ListBoxItem)ChatList.SelectedItem;
            string selectedUser = ChatList.SelectedItem.ToString();
            RemoveStartPage();

            bool alreadyMade = false;

            for (int i = 0; i < ChatTabControl.Items.Count; i++)
            {
                ChatTab theTab = (ChatTab)ChatTabControl.ItemContainerGenerator.ContainerFromIndex(i); //try to make a temp tab reference
                if (ChatList.SelectedItem == null )
                {
                    break;
                }

                //else if (theTab.tabTitle.Equals(selectedUser.Content.ToString()))
                else if (theTab.tabTitle.Equals(selectedUser))
                {
                    theTab.IsSelected = true; //so bring the tab to the front
                    break;
                }

                //else if ((i == ChatTabControl.Items.Count - 1) && (theTab.tabTitle != selectedUser.Content.ToString()))//if it is the last tab and not same
                else if ((i == ChatTabControl.Items.Count - 1) && (theTab.tabTitle != selectedUser))//if it is the last tab and not same
                {
                    //ChatTab newTab = new ChatTab(selectedUser.Content.ToString(), ChatTabControl);
                    ChatTab newTab = new ChatTab(selectedUser, ChatTabControl);
                    AddNewTab(newTab);
                    alreadyMade = true;
                    newTab.IsSelected = true;
                    break;
                }
            }

            if (ChatTabControl.Items.Count == 0 && alreadyMade == false)
            {
                ChatTab newTab = new ChatTab(selectedUser, ChatTabControl);
                AddNewTab(newTab);
                newTab.IsSelected = true;
            }
        }

        private void EndConversation_Click(object sender, RoutedEventArgs e)
        //This method is attached the click event on the end conversation menu context button
        {
            //ListBoxItem selectedUser = (ListBoxItem)ChatList.SelectedItem;
            string selectedUser = ChatList.SelectedItem.ToString();
            RemoveStartPage();

            for (int i = 0; i < ChatTabControl.Items.Count; i++)
            {
                ChatTab theTab = (ChatTab)ChatTabControl.ItemContainerGenerator.ContainerFromIndex(i); //get the current tab

                if (ChatList.SelectedItem == null)
                {
                    break;
                }

                //else if (theTab.tabTitle.Equals(selectedUser.Content.ToString())) //if the selected list item is same as tab name
                else if (theTab.tabTitle.Equals(selectedUser)) //if the selected list item is same as tab name
                {
                    if (ChatTabControl.Items.Count - 1 == 0)
                    {
                        makeStartPage();
                    }

                    ChatTabControl.Items.Remove(theTab); //remove the tab
                }
            }
        }

        private void RemoveStartPage()
        {
            for (int i = 0; i < ChatTabControl.Items.Count; i++)
            {
                // if it is a type of start tab, remove it
                if (ChatTabControl.Items[i].GetType() == typeof(StartTab)) 
                {
                    ChatTabControl.Items.Remove(ChatTabControl.Items[i]);
                    break;
                }
            }
        }

        private void makeStartPage()
        {
            ChatTabControl.Items.Add(new StartTab("Start Page", ChatTabControl));
        }

        private void AddNewTab(ChatTab newTab)
        {
            ChatTabControl.Items.Add(newTab); 
        }
    }
}